1 /*
2 * Copyright 2002-2013 the original author or authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package org.springframework.beans.factory.config;
18
19 import org.springframework.beans.factory.BeanDefinitionStoreException;
20 import org.springframework.beans.factory.BeanFactory;
21 import org.springframework.beans.factory.BeanFactoryAware;
22 import org.springframework.beans.factory.BeanNameAware;
23 import org.springframework.util.StringValueResolver;
24
25 /**
26 * Abstract base class for property resource configurers that resolve placeholders
27 * in bean definition property values. Implementations <em>pull</em> values from a
28 * properties file or other {@linkplain org.springframework.core.env.PropertySource
29 * property source} into bean definitions.
30 *
31 * <p>The default placeholder syntax follows the Ant / Log4J / JSP EL style:
32 *
33 *<pre class="code">${...}</pre>
34 *
35 * Example XML bean definition:
36 *
37 *<pre class="code">{@code
38 *<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"/>
39 * <property name="driverClassName" value="}${driver}{@code "/>
40 * <property name="url" value="jdbc:}${dbname}{@code "/>
41 *</bean>
42 *}</pre>
43 *
44 * Example properties file:
45 *
46 * <pre class="code"> driver=com.mysql.jdbc.Driver
47 * dbname=mysql:mydb</pre>
48 *
49 * Annotated bean definitions may take advantage of property replacement using
50 * the {@link org.springframework.beans.factory.annotation.Value @Value} annotation:
51 *
52 *<pre class="code">@Value("${person.age}")</pre>
53 *
54 * Implementations check simple property values, lists, maps, props, and bean names
55 * in bean references. Furthermore, placeholder values can also cross-reference
56 * other placeholders, like:
57 *
58 *<pre class="code">rootPath=myrootdir
59 *subPath=${rootPath}/subdir</pre>
60 *
61 * In contrast to {@link PropertyOverrideConfigurer}, subclasses of this type allow
62 * filling in of explicit placeholders in bean definitions.
63 *
64 * <p>If a configurer cannot resolve a placeholder, a {@link BeanDefinitionStoreException}
65 * will be thrown. If you want to check against multiple properties files, specify multiple
66 * resources via the {@link #setLocations locations} property. You can also define multiple
67 * configurers, each with its <em>own</em> placeholder syntax. Use {@link
68 * #ignoreUnresolvablePlaceholders} to intentionally suppress throwing an exception if a
69 * placeholder cannot be resolved.
70 *
71 * <p>Default property values can be defined globally for each configurer instance
72 * via the {@link #setProperties properties} property, or on a property-by-property basis
73 * using the default value separator which is {@code ":"} by default and
74 * customizable via {@link #setValueSeparator(String)}.
75 *
76 * <p>Example XML property with default value:
77 *
78 *<pre class="code">{@code
79 * <property name="url" value="jdbc:}${dbname:defaultdb}{@code "/>
80 *}</pre>
81 *
82 * @author Chris Beams
83 * @author Juergen Hoeller
84 * @since 3.1
85 * @see PropertyPlaceholderConfigurer
86 * @see org.springframework.context.support.PropertySourcesPlaceholderConfigurer
87 */
88 public abstract class PlaceholderConfigurerSupport extends PropertyResourceConfigurer
89 implements BeanNameAware, BeanFactoryAware {
90
91 /** Default placeholder prefix: {@value} */
92 public static final String DEFAULT_PLACEHOLDER_PREFIX = "${";
93
94 /** Default placeholder suffix: {@value} */
95 public static final String DEFAULT_PLACEHOLDER_SUFFIX = "}";
96
97 /** Default value separator: {@value} */
98 public static final String DEFAULT_VALUE_SEPARATOR = ":";
99
100
101 /** Defaults to {@value #DEFAULT_PLACEHOLDER_PREFIX} */
102 protected String placeholderPrefix = DEFAULT_PLACEHOLDER_PREFIX;
103
104 /** Defaults to {@value #DEFAULT_PLACEHOLDER_SUFFIX} */
105 protected String placeholderSuffix = DEFAULT_PLACEHOLDER_SUFFIX;
106
107 /** Defaults to {@value #DEFAULT_VALUE_SEPARATOR} */
108 protected String valueSeparator = DEFAULT_VALUE_SEPARATOR;
109
110 protected boolean ignoreUnresolvablePlaceholders = false;
111
112 protected String nullValue;
113
114 private BeanFactory beanFactory;
115
116 private String beanName;
117
118
119 /**
120 * Set the prefix that a placeholder string starts with.
121 * The default is {@value #DEFAULT_PLACEHOLDER_PREFIX}.
122 */
123 public void setPlaceholderPrefix(String placeholderPrefix) {
124 this.placeholderPrefix = placeholderPrefix;
125 }
126
127 /**
128 * Set the suffix that a placeholder string ends with.
129 * The default is {@value #DEFAULT_PLACEHOLDER_SUFFIX}.
130 */
131 public void setPlaceholderSuffix(String placeholderSuffix) {
132 this.placeholderSuffix = placeholderSuffix;
133 }
134
135 /**
136 * Specify the separating character between the placeholder variable
137 * and the associated default value, or {@code null} if no such
138 * special character should be processed as a value separator.
139 * The default is {@value #DEFAULT_VALUE_SEPARATOR}.
140 */
141 public void setValueSeparator(String valueSeparator) {
142 this.valueSeparator = valueSeparator;
143 }
144
145 /**
146 * Set a value that should be treated as {@code null} when
147 * resolved as a placeholder value: e.g. "" (empty String) or "null".
148 * <p>Note that this will only apply to full property values,
149 * not to parts of concatenated values.
150 * <p>By default, no such null value is defined. This means that
151 * there is no way to express {@code null} as a property
152 * value unless you explicitly map a corresponding value here.
153 */
154 public void setNullValue(String nullValue) {
155 this.nullValue = nullValue;
156 }
157
158 /**
159 * Set whether to ignore unresolvable placeholders.
160 * <p>Default is "false": An exception will be thrown if a placeholder fails
161 * to resolve. Switch this flag to "true" in order to preserve the placeholder
162 * String as-is in such a case, leaving it up to other placeholder configurers
163 * to resolve it.
164 */
165 public void setIgnoreUnresolvablePlaceholders(boolean ignoreUnresolvablePlaceholders) {
166 this.ignoreUnresolvablePlaceholders = ignoreUnresolvablePlaceholders;
167 }
168
169 /**
170 * Only necessary to check that we're not parsing our own bean definition,
171 * to avoid failing on unresolvable placeholders in properties file locations.
172 * The latter case can happen with placeholders for system properties in
173 * resource locations.
174 * @see #setLocations
175 * @see org.springframework.core.io.ResourceEditor
176 */
177 @Override
178 public void setBeanName(String beanName) {
179 this.beanName = beanName;
180 }
181
182 /**
183 * Only necessary to check that we're not parsing our own bean definition,
184 * to avoid failing on unresolvable placeholders in properties file locations.
185 * The latter case can happen with placeholders for system properties in
186 * resource locations.
187 * @see #setLocations
188 * @see org.springframework.core.io.ResourceEditor
189 */
190 @Override
191 public void setBeanFactory(BeanFactory beanFactory) {
192 this.beanFactory = beanFactory;
193 }
194
195
196 protected void doProcessProperties(ConfigurableListableBeanFactory beanFactoryToProcess,
197 StringValueResolver valueResolver) {
198
199 BeanDefinitionVisitor visitor = new BeanDefinitionVisitor(valueResolver);
200
201 String[] beanNames = beanFactoryToProcess.getBeanDefinitionNames();
202 for (String curName : beanNames) {
203 // Check that we're not parsing our own bean definition,
204 // to avoid failing on unresolvable placeholders in properties file locations.
205 if (!(curName.equals(this.beanName) && beanFactoryToProcess.equals(this.beanFactory))) {
206 BeanDefinition bd = beanFactoryToProcess.getBeanDefinition(curName);
207 try {
208 visitor.visitBeanDefinition(bd);
209 }
210 catch (Exception ex) {
211 throw new BeanDefinitionStoreException(bd.getResourceDescription(), curName, ex.getMessage(), ex);
212 }
213 }
214 }
215
216 // New in Spring 2.5: resolve placeholders in alias target names and aliases as well.
217 beanFactoryToProcess.resolveAliases(valueResolver);
218
219 // New in Spring 3.0: resolve placeholders in embedded values such as annotation attributes.
220 beanFactoryToProcess.addEmbeddedValueResolver(valueResolver);
221 }
222
223 }